import 'package:winkai/db/db_ext.dart';
import 'package:drift/drift.dart';

import '../database.dart';
import '../tables/act_tb.dart';

part 'act_dao.g.dart';

@DriftAccessor(tables: [ActTb])
class ActDao extends DatabaseAccessor<RootDatabase> with _$ActDaoMixin {
  ActDao(super.attachedDatabase);

  Future<int> insertOrUpdate({
    required String deviceId,
    String? nickname,
    String? serviceUserId,
    String? avatar,
  }) async {
    return into(actTb).insertOnConflictUpdate(ActTbCompanion(
      id: Value(deviceId),
      nickname: Value(nickname),
      serviceUserId: serviceUserId.toDrifitNullSafeValue,
      avatar: Value(avatar),
    ));
  }

  Future<ActTbData?> getCurrentAccount({String? deviceId}) {
    return (select(actTb)..where((tbl) => tbl.id.equalsNullable(deviceId)))
        .getSingleOrNull();
  }
}
